﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Web;

namespace System
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class Extension
    {
        /// <summary>
        /// 指定格式化字符串序列格式化当前字符串实例。
        /// </summary>
        /// <param name="value">当前字符串实例</param>
        /// <param name="args">格式化字符串序列</param>
        /// <returns></returns>
        public static string FormatWith(this string value, params object[] args)
        {
            return string.Format(value, args);
        }

        /// <summary>
        /// 是否为Null
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool IsNull(this string value)
        {
            return (value == null);
        }

        /// <summary>
        /// 是否为Empty
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool IsEmpty(this string value)
        {
            return string.IsNullOrEmpty(value) || value.Trim().Length == 0;
        }

        /// <summary>
        /// 如果为空
        /// </summary>
        /// <param name="value"></param>
        /// <param name="default"></param>
        /// <returns></returns>
        public static string IfEmpty(this string value, string @default)
        {
            return value.IsEmpty() ? @default : value;
        }

        /// <summary>
        /// 是否相等
        /// </summary>
        /// <param name="value"></param>
        /// <param name="value2"></param>
        /// <returns></returns>
        public static bool Compare(this string value, string value2)
        {
            return string.Compare(value, value2, true) == 0;
        }

        /// <summary>
        /// 获取左N位
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="count">长度</param>
        /// <returns></returns>
        public static string Left(this string value, int count)
        {
            return value.Substring(0, count);
        }

        /// <summary>
        /// 获取右N位
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="count">长度</param>
        /// <returns></returns>
        public static string Right(this string value, int count)
        {
            return value.Substring(value.Length - count);
        }

        /// <summary>
        /// Split
        /// </summary>
        /// <param name="value"></param>
        /// <param name="regexPattern"></param>
        /// <returns></returns>
        public static string[] Split(this string value, string regexPattern)
        {
            return value.Split(regexPattern, RegexOptions.None);
        }

        /// <summary>
        /// Split
        /// </summary>
        /// <param name="value"></param>
        /// <param name="regexPattern"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string[] Split(this string value, string regexPattern, RegexOptions options)
        {
            return Regex.Split(value, regexPattern, options);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static string MD5(this string value)
        {
            return value.MD5(32);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="c">16or32</param>
        /// <returns></returns>
        public static string MD5(this string value, int c)
        {
            string str = string.Empty;
            if (string.IsNullOrEmpty(value))
            {
                return str;
            }
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = Encoding.Default.GetBytes(value);
            byte[] result = md5.ComputeHash(data);
            for (int i = 0; i < result.Length; i++)
            {
                str += result[i].ToString("x").PadLeft(2, '0');
            }
            if (c == 16)
            {
                str = str.Substring(8, 16);
            }
            return str;
        }

        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static string SHA1(this string value)
        {
            string str = string.Empty;
            if (string.IsNullOrEmpty(value))
            {
                return str;
            }
            SHA1Managed sha1 = new SHA1Managed();
            byte[] data = System.Text.Encoding.Default.GetBytes(value);
            byte[] result = sha1.ComputeHash(data);
            for (int i = 0; i < result.Length; i++)
            {
                str += result[i].ToString("x").PadLeft(2, '0');
            }
            return str;
        }

        /// <summary>
        /// 字符串转字符串
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="default">默认值</param>
        /// <returns></returns>
        public static string ToString(this string value, string @default)
        {
            if (string.IsNullOrEmpty(value))
            {
                return @default;
            }
            else
            {
                return value;
            }
        }

        /// <summary>
        /// 字符串转整型
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static int ToInt(this string value)
        {
            return value.ToInt(0);
        }

        /// <summary>
        /// 字符串转整型
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="default">默认值</param>
        /// <returns></returns>
        public static int ToInt(this string value, int @default)
        {
            if (string.IsNullOrEmpty(value))
            {
                return @default;
            }
            int i;
            if (int.TryParse(value, out i))
            {
                return i;
            }
            return @default;
        }

        /// <summary>
        /// 字符串转布尔值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool ToBoolean(this string value)
        {
            return value.ToBoolean(false);
        }

        /// <summary>
        /// 字符串转布尔值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="default">默认值</param>
        /// <returns></returns>
        public static bool ToBoolean(this string value, bool @default)
        {
            if (string.IsNullOrEmpty(value))
            {
                return @default;
            }
            if (string.Compare(value, "y", true) == 0 ||
                string.Compare(value, "yes", true) == 0 ||
                string.Compare(value, "true", true) == 0 ||
                value.ToInt() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 字符串转时间
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string value)
        {
            return value.ToDateTime(DateTime.Now);
        }

        /// <summary>
        /// 字符串转时间
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="default">默认时间</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string value, DateTime @default)
        {
            if (string.IsNullOrEmpty(value))
            {
                return @default;
            }
            DateTime now;
            if (!DateTime.TryParse(value, out now))
            {
                now = @default;
            }
            return now;
        }

        /// <summary>
        /// 是否在素组里面
        /// </summary>
        /// <param name="value">数组</param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool InArray(this object[] value, object s)
        {
            if (value == null || value.Length == 0)
            {
                return false;
            }
            foreach (object obj in value)
            {
                if (obj.Equals(s))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// HtmlEncode
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string HtmlEncode(this string value)
        {
            return HttpContext.Current.Server.HtmlEncode(value);
        }


        /// <summary>
        /// HtmlDecode
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static string HtmlDecode(this string value)
        {
            return HttpContext.Current.Server.HtmlDecode(value);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns>编码结果</returns>
        public static string UrlEncode(this string value)
        {
            return HttpUtility.UrlEncode(value);
        }

        /// <summary>
        /// 返回 URL 字符串的编码结果
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns>解码结果</returns>
        public static string UrlDecode(this string value)
        {
            return HttpUtility.UrlDecode(value);
        }

        /// <summary>
        /// 时间戳转时间
        /// </summary>
        /// <param name="value">时间戳</param>
        /// <returns></returns>
        public static DateTime GetDateTime(this int value)
        {
            if (value < 1000000000)
            {
                return DateTime.Now;
            }
            DateTime baseDateTime = DateTime.Parse("1970-1-01 08:00:00.000");
            long i = (long)value * 1000;
            return baseDateTime.AddTicks(i * TimeSpan.TicksPerMillisecond);
        }

        /// <summary>
        /// 时间转时间戳
        /// </summary>
        /// <param name="value">时间</param>
        /// <returns></returns>
        public static int GetTimeMillis(this DateTime value)
        {
            DateTime baseDateTime = DateTime.Parse("1970-1-01 08:00:00.000");
            return (int)((value - baseDateTime).TotalMilliseconds / 1000);
        }

        /// <summary>
        /// 是否存在权限
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool Has<T>(this Enum type, T value)
        {
            try
            {
                return (((int)(object)type & (int)(object)value) == (int)(object)value);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 判断权限
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool Is<T>(this Enum type, T value)
        {
            try
            {
                return (int)(object)type == (int)(object)value;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Add<T>(this Enum type, T value)
        {
            try
            {
                return (T)(object)(((int)(object)type | (int)(object)value));
            }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("不能添加类型 '{0}'", typeof(T).Name), ex);
            }
        }

        /// <summary>
        /// 移除权限
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Remove<T>(this Enum type, T value)
        {
            try
            {
                return (T)(object)(((int)(object)type & ~(int)(object)value));
            }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("不能移除类型 '{0}'", typeof(T).Name), ex);
            }
        }
    }
}
